// 2013-9-12上午02:21:11  net.betou.trade.main.dao.agency.impl.DaoImpl.java

package net.betou.trade.main.dao.agencyorproxy.impl;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.main.dao.agencyorproxy.Dao;

/**
 * 
 * @author 叶装装
 */
@Repository
public class DaoImpl extends HibernateBaseDao< AgentAndAdmins, Integer > implements Dao{

	public int countByEmail( String email ) {
		return count( "email", email );
	}

	public int countByMobile( String mobile ) {
		return count( "mobile", mobile );
	}

	public int countByName( String name ) {
		return count( "intermediaName", name );
	}

	public int countByUsername( String username ) {
		return count( "username", username );
	}

	public AgentAndAdmins deleteById( Integer id ) {
		AgentAndAdmins bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}

		return bean;
	}

	public AgentAndAdmins findByUsername( String username ) {
		return findUniqueByProperty( "username", username );
	}
	
	public AgentAndAdmins findByIntermediaName( String intermediaName ){
		return findUniqueByProperty( "intermediaName", intermediaName );
	}

	public AgentAndAdmins save( AgentAndAdmins bean ) {
		getSession( ).save( bean );
		return bean;
	}

	public AgentAndAdmins findById( Integer id ) {
		AgentAndAdmins bean = get( id );
		return bean;
	}

	public Pagination getPage( Boolean system, Boolean justAdmin, Boolean agencyOrProxy, Integer selfId, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize, String filterStatus ) {

		Finder f = Finder.create( "select bean from AgentAndAdmins bean where 1=1 " );

		if( system != null ){
			f.append( " and bean.system=:system " );
			f.setParam( "system", system );
		}

		if( justAdmin != null ){
			f.append( " and bean.justAdmin=:justAdmin " );
			f.setParam( "justAdmin", justAdmin );
		}

		if( agencyOrProxy != null ){
			f.append( " and bean.agencyOrProxy=:agencyOrProxy" );
			f.setParam( "agencyOrProxy", agencyOrProxy );
		}

		if( selfId != null ){
			f.append( " and bean.parent.id =:selfId " );
			f.setParam( "selfId", selfId );
		}

		createFilter( f, filterStatus );
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );

		return find( f, pageNo, pageSize );
	}

	private int count( String name, String value ) {
		String hql = "select count(*) from AgentAndAdmins bean where bean." + name + "=:" + name;
		Query query = getSession( ).createQuery( hql );
		query.setParameter( name, value );
		return ( ( Number ) query.iterate( ).next( ) ).intValue( );
	}

	@Override
	protected Class< AgentAndAdmins > getEntityClass( ) {
		return AgentAndAdmins.class;
	}

}
